package com.zhengaz.service.es.controller;

import cn.hutool.core.util.StrUtil;
import com.zhengaz.common.core.entity.R;
import com.zhengaz.service.es.entity.Goods;
import com.zhengaz.service.es.service.BasicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author zhengzg
 * @create 2023-09-18 14:34
 */
@Api(tags = "ES-基础控制器")
@RestController
@RequestMapping("/api/es")
public class BasicController {

    @Resource
    private BasicService basicService;

    @ApiOperation("添加数据")
    @GetMapping("/createIndex")
    public R<Boolean> createIndex() {
        return R.ok(basicService.createIndex("test"));
    }

    @ApiOperation("添加数据")
    @PostMapping("/add")
    public R<Boolean> add() {
        Goods goods = Goods.builder().name("华为mate60 pro")
                .id(StrUtil.uuid())
                .price(new BigDecimal("6999"))
                .color("月牙白")
                .build();
        return R.ok(basicService.add("test", goods));
    }

    @ApiOperation("查询数据")
    @GetMapping("/list")
    public R<List<Goods>> list() {
        return R.ok(basicService.search("test"));
    }

    @ApiOperation("修改数据")
    @PutMapping("/update")
    public R<Boolean> update(String id) {
        Goods goods = Goods.builder().name("华为mate60 pro")
                .id(StrUtil.uuid())
                .price(new BigDecimal("7999"))
                .color("天蓝")
                .build();
        return R.ok(basicService.update("test", id, goods));
    }

    @ApiOperation("删除数据")
    @PutMapping("/delete")
    public R<Boolean> delete(String id) {
        return R.ok(basicService.delete("test", id));
    }

}
